import java.util.Hashtable;

import org.apache.log4j.Logger;

public class Lock {
	public static Object lock = new Object();
	private static Hashtable<String, Counter> table = new Hashtable<String, Counter>();
	private static Logger logger = Logger.getLogger("Lock");
	public static Object getLock(String processInstanceId) {
		if (table.get(processInstanceId) == null) {
			table.put(processInstanceId, new Counter());
			return table.get(processInstanceId);
		} else {
			Counter counter = (Counter) table.get(processInstanceId);
			counter.increase();
			return table.get(processInstanceId);
		}
	}

	public static void deleteLock(String processInstanceId) {
		if (table.get(processInstanceId) != null) {
			Counter counter = (Counter) table.get(processInstanceId);
			counter.decrease();
			if (counter.getCount()==0) {
				table.remove(processInstanceId);				
				//logger.info("Remove counter "+table.size());
				//logger.info(table.toString());
			}
		}
	}	
}
 class Counter
{
	int count;
	
	public Counter(){
		count=1;
	}
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	public void increase()
	{
		this.count++;
	}
	
	public void decrease()
	{
		this.count--;
	}		
}